﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string SaleDetail_Add(SaleDetail model ,int ACID);


[OperationContract]
string SaleDetail_Update(SaleDetail model ,int ACID);


[OperationContract]
string SaleDetailBysaledid_Delete(int saledid,int ACID);


[OperationContract]
SaleDetail SaleDetailBysaledid_Get(int saledid,int ACID );


[OperationContract]
List<SaleDetail> SaleDetail_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加SaleDetail表数据
/// <summary>
/// 添加SaleDetail
/// </summary>
/// <param name="model">SaleDetail实体</param>
/// <returns>空或错误</returns>
public string SaleDetail_Add(SaleDetail model,int ACID)
{
if(model.saledid==null )
{
return "『int』不能为空！";
}
if(model.ispresent==null )
{
return "『是否赠品』不能为空！";
}
if(model.goodsamt==null )
{
return "『总价』不能为空！";
}
if(model.unitqty==null )
{
return "『数量』不能为空！";
}
if(model.unitprice==null )
{
return "『单价』不能为空！";
}
if(model.quantity==null )
{
return "『基本数量』不能为空！";
}
if(model.price==null )
{
return "『基本单价』不能为空！";
}
if(model.aprice==null )
{
return "『成本单价』不能为空！";
}
if(model.disc==null )
{
return "『折扣』不能为空！";
}
if(string.IsNullOrEmpty(model.GoodsCode))
{
return "『产品编号』不能为空！";
}
if(model.invoiceid==null )
{
return "『销售单ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.SaleDetail.Count(p=>p.saledid==model.saledid)>0)
{
return "主键已存在！";
}
ctx.SaleDetail.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加SaleDetail表数据事物用重构
/// <summary>
/// 添加SaleDetail事物用重构
/// </summary>
/// <param name="model">SaleDetail实体</param>
/// <returns>空或错误</returns>
private string SaleDetail_Add(SaleDetail model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.saledid==null )
{
return "『int』不能为空！";
}
if(model.ispresent==null )
{
return "『是否赠品』不能为空！";
}
if(model.goodsamt==null )
{
return "『总价』不能为空！";
}
if(model.unitqty==null )
{
return "『数量』不能为空！";
}
if(model.unitprice==null )
{
return "『单价』不能为空！";
}
if(model.quantity==null )
{
return "『基本数量』不能为空！";
}
if(model.price==null )
{
return "『基本单价』不能为空！";
}
if(model.aprice==null )
{
return "『成本单价』不能为空！";
}
if(model.disc==null )
{
return "『折扣』不能为空！";
}
if(string.IsNullOrEmpty(model.GoodsCode))
{
return "『产品编号』不能为空！";
}
if(model.invoiceid==null )
{
return "『销售单ID』不能为空！";
}
if(ctx.SaleDetail.Count(p=>p.saledid==model.saledid)>0)
{
return "主键已存在！";
}
ctx.SaleDetail.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改SaleDetail表数据
/// <summary>
/// 修改SaleDetail
/// </summary>
/// <param name="model">SaleDetail实体</param>
/// <returns>空或错误</returns>
public string SaleDetail_Update(SaleDetail model,int ACID)
{
if(model.saledid==null )
{
return "『int』不能为空！";
}
if(model.ispresent==null )
{
return "『是否赠品』不能为空！";
}
if(model.goodsamt==null )
{
return "『总价』不能为空！";
}
if(model.unitqty==null )
{
return "『数量』不能为空！";
}
if(model.unitprice==null )
{
return "『单价』不能为空！";
}
if(model.quantity==null )
{
return "『基本数量』不能为空！";
}
if(model.price==null )
{
return "『基本单价』不能为空！";
}
if(model.aprice==null )
{
return "『成本单价』不能为空！";
}
if(model.disc==null )
{
return "『折扣』不能为空！";
}
if(string.IsNullOrEmpty(model.GoodsCode))
{
return "『产品编号』不能为空！";
}
if(model.invoiceid==null )
{
return "『销售单ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.SaleDetail.Count(p=>p.saledid==model.saledid)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
SaleDetail SaleDetailmodel=ctx.SaleDetail.Single(p => p.saledid==model.saledid);
if(model.saledid!=null )
{
SaleDetailmodel.saledid=model.saledid;
}
if(model.invoiceid!=null )
{
SaleDetailmodel.invoiceid=model.invoiceid;
}
if(!string.IsNullOrEmpty(model.GoodsCode))
{
SaleDetailmodel.GoodsCode=model.GoodsCode;
}
if(!string.IsNullOrEmpty(model.batchcode))
{
SaleDetailmodel.batchcode=model.batchcode;
}
if(model.disc!=null )
{
SaleDetailmodel.disc=model.disc;
}
if(model.aprice!=null )
{
SaleDetailmodel.aprice=model.aprice;
}
if(model.price!=null )
{
SaleDetailmodel.price=model.price;
}
if(model.quantity!=null )
{
SaleDetailmodel.quantity=model.quantity;
}
if(model.unitprice!=null )
{
SaleDetailmodel.unitprice=model.unitprice;
}
if(model.unitqty!=null )
{
SaleDetailmodel.unitqty=model.unitqty;
}
if(model.goodsamt!=null )
{
SaleDetailmodel.goodsamt=model.goodsamt;
}
if(!string.IsNullOrEmpty(model.userdef1))
{
SaleDetailmodel.userdef1=model.userdef1;
}
if(!string.IsNullOrEmpty(model.userdef2))
{
SaleDetailmodel.userdef2=model.userdef2;
}
if(!string.IsNullOrEmpty(model.userdef3))
{
SaleDetailmodel.userdef3=model.userdef3;
}
if(model.ispresent!=null )
{
SaleDetailmodel.ispresent=model.ispresent;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除SaleDetail表Bysaledid数据
/// <summary>
/// 删除SaleDetailBysaledid
/// </summary>
/// <param name="saledid">int</param>
/// <returns>空或错误</returns>
public string SaleDetailBysaledid_Delete(int saledid,int ACID)
{
if(saledid==null )
{
return "『int』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.SaleDetail.Count(p=>p.saledid==saledid)!=1)
{
return null;
}
SaleDetail model=ctx.SaleDetail.Single(p => p.saledid== saledid);
ctx.SaleDetail.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除SaleDetail表Bysaledid数据[事物用重构]
/// <summary>
/// 删除SaleDetailBysaledid[事物用重构]
/// </summary>
/// <param name="saledid">int</param>
/// <returns>空或错误</returns>
private string SaleDetailBysaledid_Delete(int saledid,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(saledid==null )
{
return "『int』不能为空！";
}
if(ctx.SaleDetail.Count(p=>p.saledid==saledid)!=1)
{
return null;
}
SaleDetail model=ctx.SaleDetail.Single(p => p.saledid== saledid);
ctx.SaleDetail.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取SaleDetail表Bysaledid数据
/// <summary>
/// 获取SaleDetailBysaledid
/// </summary>
/// <param name="saledid">int</param>
/// <returns>当前表</returns>
public SaleDetail SaleDetailBysaledid_Get(int saledid,int ACID)
{
if(saledid==null )
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.SaleDetail.Count(p=>p.saledid==saledid)!=1)
{
return null;
}
return ctx.SaleDetail.SingleOrDefault(p=>p.saledid==saledid);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取SaleDetail表数据
/// <summary>
/// 获取SaleDetail
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<SaleDetail></returns>
public List<SaleDetail> SaleDetail_Get(Expression<Func<SaleDetail, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.SaleDetail.ToList();
}
 else 
{
return ctx.SaleDetail.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.SaleDetail.Where(predicate).ToList();
}
 else 
{
return ctx.SaleDetail.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取SaleDetail表数据
/// <summary>
/// 获取SaleDetail
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<SaleDetail></returns>
public List<SaleDetail> SaleDetail_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return SaleDetail_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取SaleDetail表数据计数
/// <summary>
/// 获取SaleDetail表数据计数
/// </summary>
/// <returns>List<SaleDetail></returns>
public int SaleDetail_GetCount(Expression<Func<SaleDetail, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.SaleDetail.Count();

}
 else
{
return ctx.SaleDetail.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取SaleDetail分页表数据
/// <summary>
/// 获取SaleDetail分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<SaleDetail></returns>
public List<SaleDetail> SaleDetail_Getpage(int pageIndex, int pageSize,Expression<Func<SaleDetail, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.SaleDetail.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.SaleDetail.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.SaleDetail.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.SaleDetail.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改SaleDetail表数据
/// <summary>
/// 批量修改SaleDetail
/// </summary>
/// <param name="predicate">Expression<Func<SaleDetail, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<SaleDetail,SaleDetail>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int SaleDetail_batchUpdate(Expression<Func<SaleDetail, bool>> predicate,Expression<Func<SaleDetail, SaleDetail>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.SaleDetail.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改SaleDetail表数据事物用重构
/// <summary>
/// 批量修改SaleDetail事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<SaleDetail, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<SaleDetail,SaleDetail>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int SaleDetail_batchUpdate(Expression<Func<SaleDetail, bool>> predicate,Expression<Func<SaleDetail, SaleDetail>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.SaleDetail.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除SaleDetail表数据
/// <summary>
/// 批量删除SaleDetail
/// </summary>
/// <param name="predicate">Expression<Func<SaleDetail, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int SaleDetail_batchDel(Expression<Func<SaleDetail, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.SaleDetail.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除SaleDetail表数据事物用重构
/// <summary>
/// 批量删除SaleDetail事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<SaleDetail, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int SaleDetail_batchDel(Expression<Func<SaleDetail, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.SaleDetail.Delete(predicate);
return ok;
}
#endregion


}
}

